<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="冻结日期">
        <el-date-picker v-model="queryParams.time" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['produce:freeze:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" height="calc(100vh - 240px)" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="单据编号" align="center" prop="documentNumber" />

      <el-table-column label="工单编号" align="center" prop="workorderCode" />
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="productSpc" />
      <el-table-column label="冻结时间" align="center" prop="createTime" />

      <!-- <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="handleBand(scope.row)" type="text" size="small"
            >绑定</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { reqList } from "@/api/mes/pro/materialFreeze";
import { dataTool } from "echarts";

export default {
  name: "MaterialFreeze",
  dicts: ["sys_yes_no"],
  data() {
    let validateReason = (rule, value, callback) => {
      setTimeout(() => {
        if (this.form.status === 1) {
          if (value === 0 || value === 1 || value === 2 || value === 3) {
            callback();
          } else {
            callback(new Error("状态是不可维修时，原因必填！"));
          }
        } else {
          callback();
        }
      }, 100);
    };
    let validateStatus = (rule, value, callback) => {
      setTimeout(() => {
        if (value === 1) {
          this.$refs.form.validateField("reason");
          callback();
        } else if (value === 0) {
          callback();
        } else {
          callback(new Error("状态必填！"));
        }
      }, 100);
    };
    return {
      selection: [],
      repairTypeList: [
        {
          value: 0,
          label: "售后维修",
        },
        {
          value: 1,
          label: "制程维修",
        },
      ],
      inTypeList: [
        {
          value: 0,
          label: "售后",
        },
        {
          value: 1,
          label: "产线",
        },
        {
          value: 2,
          label: "不良品仓",
        },
      ],
      statusList: [
        //'可维修','不可维修'
        {
          value: 0,
          label: "可维修",
        },
        {
          value: 1,
          label: "不可维修",
        },
      ],
      reasonList: [
        // reasonList:['缺物料','无法修复','维修时间过长','报废']
        {
          value: 0,
          label: "缺物料",
        },
        {
          value: 1,
          label: "无法修复",
        },
        {
          value: 2,
          label: "维修时间过长",
        },
        {
          value: 3,
          label: "报废",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修工单 表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      title1: "修改维修工单",
      title2: "判定维修工单",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        timeBegin: null,
        timeEnd: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        snCode: [
          { required: true, message: "sn编码不能为空", trigger: "blur" },
        ],
        number: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        number: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" },
          { validator: validateStatus, trigger: "change" },
        ],
        reason: [{ validator: validateReason, trigger: "change" }],
        repairType: [
          { required: true, message: "维修类型不能为空", trigger: "change" },
        ],
        repairHour: [
          { required: true, message: "维修工时  不能为空", trigger: "blur" },
        ],
        inType: [
          { required: true, message: "入库类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() { },
  methods: {
    /** 导出按钮操作 */
    handleExport() {
      console.log(this.queryParams, "=====queryParams===");
      if (this.queryParams.time) {
        this.queryParams["timeBegin"] = this.queryParams.time[0];
        this.queryParams["timeEnd"] = this.queryParams.time[1];
      } else {
        this.queryParams["timeBegin"] = null;
        this.queryParams["timeEnd"] = null;
      }

      this.download(
        "/mes/wm/wmMaterialFreeze/export",
        {
          ...this.queryParams,
        },
        `calplan_${new Date().getTime()}.xlsx`
      );
    },
    handleSelectionChange(val) {
      console.log(111);
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    handleGetworkorderSn() {
      console.log(this.form.snCode);
      getworkorderSn(this.form.snCode).then((res) => {
        const data = res.data ? res.data : {};
        this.form.workorderCode = data.workorderCode;
        this.form.itemCode = data.productCode;
        this.form.itemName = data.productName;
        this.form.specification = data.productSpec;
      });
    },
    /** 查询维修工单 列表 */
    getList() {
      this.loading = true;
      reqList(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workorderCode: null,
        itemCode: null,
        itemName: null,
        specification: null,
        number: null,
        snCode: null,
        createBy: null,
        createTime: null,
        status: null,
        reason: null,
        updateBy: null,
        updateTime: null,
        repairType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams, "=====queryParams===");
      if (this.queryParams.time) {
        this.queryParams["timeBegin"] = this.queryParams.time[0];
        this.queryParams["timeEnd"] = this.queryParams.time[1];
      } else {
        this.queryParams["timeBegin"] = null;
        this.queryParams["timeEnd"] = null;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.time = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map((item) => item.id);
    //   this.selection = selection;
    //   this.single = selection.length !== 1;
    //   this.multiple = !selection.length;
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修工单 ";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row.id
        ? this.deepClone(row)
        : this.deepClone(this.selection[0]);
      this.open = true;
      this.title = this.title1;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 判定按钮操作 */
    handleDetermin(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row.id ? row : this.selection[0];
      this.open = true;
      this.title = this.title2;
    },
    determineForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateOrder(this.form).then((response) => {
            this.$modal.msgSuccess("判定成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
